package tutorial.utils;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

/**
 * @Author: dingziyuan
 * @Date: 2023/11/3/003 16:32
 * @Description: *
 */
public class InterfaceUtils {

    private static String usdCny;

    /**
     * 获取实时汇率
     * @return
     */
    public static String getExchangeRate() {
        String url = "https://www.okx.com/api/v5/market/exchange-rate";
        final String resData = HttpUtil.get(url);
        final JSONObject entries = JSONUtil.parseObj(resData);
        if ("50011".equals(entries.get("code",String.class))) {
            return usdCny;
        }
        final JSONArray data = JSONUtil.parseArray(entries.get("data"));
        final JSONObject entries1 = JSONUtil.parseObj(data.get(0));
        usdCny = entries1.get("usdCny", String.class);
        return entries1.get("usdCny", String.class);
    }
}
